package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.entity.OrderState;
import com.bto.solar.work.service.BtoDesignService;
import com.bto.solar.work.service.BtoDeviceService;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author 王小波
 * @description: 初设评审处理器
 * @date 2023/7/24 8:18
 */

@Slf4j
@Component
public class DesignStageHandler implements StageHandler {
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private BtoDeviceService btoDeviceService;
    @Autowired
    private BtoDesignService btoDesignService;
    @Autowired
    @Lazy
    private ShiGongStageHandler shiGongStageHandler;

    private String stageId = BizStageEnum.DESIGN.getStageId();

    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId , BizStageEnum.DESIGN.getStageId() , BizTaskEnum.TASK_CSPSXX.getTaskId(),
                BizTaskStateEnum.WAITING_FILLED.getStateId());
    }

    /**
    * 设计-待审核
     * 1、初次提交审核 2、审批拒绝待审核 3、设计变更待审核
    */
    @Override
    public void updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.DESIGN.getStageId(), taskId,
                BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(),
                BizTaskStateEnum.WAITING_FILLED.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId(),
                BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId());


        btoDesignService.updateLatestDesignState(orderId , BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId());

    }

    @Transactional
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId  , String approvalType) {
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.DESIGN.getStageId() ,  taskId ,
                BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId()  , true);
        //设计通过时，触发设备信息的更新
        btoDeviceService.updateDesignDeviceInfo(orderId);
        //记录设计变更通过
        btoDesignService.updateLatestDesignState(orderId , BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId());
        //开启施工阶段
        shiGongStageHandler.createNewInitState(orderId , BizStageEnum.CONSTRUCT.getStageId() , null);
    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId , String approvalType  , Object rejectInfo) {
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.DESIGN.getStageId() , taskId ,
                BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId() , true);

        btoDesignService.updateLatestDesignState(orderId , BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId());
    }

    /**
    *  判断是否可以创建设计变更
     *  1、初设评审通过
     *  2、设计变更通过
    */
    public boolean canCreateNewDesignUpdate(String orderId){
        OrderState approvalPassBtoState = btoOrderStateService.getOrderState(orderId, this.stageId, BizTaskEnum.TASK_CSPSXX.getTaskId() , BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId());
        OrderState updateApprovalPassBtoState = btoOrderStateService.getOrderState(orderId, this.stageId, BizTaskEnum.TASK_CSPSXX_UPDATE.getTaskId() , BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId());
        //设计和更新都没通过
        if(approvalPassBtoState == null && updateApprovalPassBtoState == null){
            return false;
        }
        return true;
    }

    /**
    * 设计审核通过后，创建设计变更状态
    */
    public void createDesignUpdateState(String orderId){
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.DESIGN.getStageId() ,  BizTaskEnum.TASK_CSPSXX_UPDATE.getTaskId() ,
                BizTaskStateEnum.WAITING_FILLED.getStateId() ,
                BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId()  , false);
        //btoDesignService.updateLatestDesignState(orderId , BizTaskStateEnum.WAITING_FILLED.getStateId());
    }

    /**
     * 设计审核通过后，创建设计变更状态
     */
    public boolean isDesignStagePass(String orderId){
        OrderState designUpdate = btoOrderStateService.getOrderTaskState(orderId, BizStageEnum.DESIGN.getStageId(),
                BizTaskEnum.TASK_CSPSXX_UPDATE.getTaskId());
        List<String> passState = Arrays.asList(BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId(), BizTaskStateEnum.WAITING_FILLED.getStateId());
        //经过了设计变更
        if(NullUtils.isNotNull(designUpdate)){
            return passState.contains(designUpdate.getStateId());
        }
        //没有经过设计变更
        else {
            OrderState designPass = btoOrderStateService.getOrderState(orderId, BizStageEnum.DESIGN.getStageId(), BizTaskEnum.TASK_CSPSXX.getTaskId(),
                    BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId());
            return NullUtils.isNotNull(designPass);
        }
    }

}
